<?php

class Courses {
  function __construct() {
  }

  function getCourseList() {
    global $page_handler;

    $cdata = $page_handler->myCourses();
    $cpath = '/html/body/form/table/tr[2]/td[3]/table/tr[4]/td/div/div/table[2]/tr/td/table/tr/td/table/tr/td/table/tr';
    $clist = xPathify($cdata, $cpath);

    $results      = array(array());
    $count        = 0;
    $extra_course = false;
    $clist_length = $clist->length;
    for ($i = 3; $i < $clist_length; $i++) {
      if (strpos($clist->item($i)->nodeValue, "Valda kurser utanför mina studieplaner")) {
        $i += 2;
        $extra_course = true;
      }

      if ($i % 2 == 1) {
        $ccode       = xPathify($cdata, $cpath . '['. ($i + 1) . ']/td/span');
        $course_code = $ccode->item(0)->getAttributeNode('onclick')->nodeValue;
        $course_code = preg_split("/[a-zA-Z',:_()]+/", $course_code);

        $course = $clist->item($i)->nodeValue;
        $course = preg_split("/[0-9,.]+/", $course);

        $results[$count]['code'] = $course_code[1];
        $results[$count]['name'] = trim($course[1]);

        if ($extra_course) {
          $results[$count]['extra'] = 1;
        }

        $count++;
      }
    }

    return $results;
  }

  function getExaminationList() {
    global $page_handler;

    $edata = $page_handler->myExaminations();
    $epath = '/html/body/form/table/tr[2]/td[3]/table/tr[4]/td/div/div/table[2]/tr/td/table/tr/td/table/tr[1]/td/table/tr';
    $elist = xPathify($edata, $epath);

    $results = array(array());
    $elist_length = $elist->length;
    for ($i = 2; $i <= $elist_length; $i++) {
      $exam_path = $epath . "[$i]/td";
      $exam      = xPathify($edata, $exam_path);

      $examdate  = trim($exam->item(0)->nodeValue);
      $examdata  = trim($exam->item(1)->nodeValue);
      $active    = trim($exam->item(2)->nodeValue);
      $status    = trim($exam->item(3)->nodeValue);
      $completed = trim($exam->item(4)->nodeValue);
      $enrol     = trim($exam->item(5)->nodeValue);

      $name = explode ("[",$examdata);
      $name = preg_replace('/\h+/', ' ', $name[0]);
      $name = preg_replace('/\v{3,}/', ' ', $name);

      if (stristr($enrol, 'Anmäl')) {
        $enrol = true;
      }
      else {
        $enrol = false;
      }

      $results[] = array(
        'exam_date'      => $examdate,
        'exam_name'      => $name,
        'exam_active'    => $active,
        'exam_status'    => $status,
        'exam_completed' => $completed,
        'exam_enrol'     => $enrol
      );
    }

    return $results;
  }

  function getCourseInfo($course_id) {
  }

  function getExaminationInfo() {
  }

  function searchCourses() {
  }

  function searchExaminations() {
  }

  function removeCourse() {
  }

  function dropCourse() {
  }

  function removeExamination() {
  }

  function enrollExamination() {
  }

  function dropExamination() {
  }
}
